/**
 * 	@author btan
 *	@email: btan@tma.com.vn - anbtan@gmail.com
 */
package com.vital.mshopping;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class ImageAdapter extends BaseAdapter{
	private Context mContext;
	
	public Integer[] mDrawableIds; 
	public String[] mDrawables;
	
	//---Constructor---
	public ImageAdapter(Context ctx) {
		this.mContext = ctx;
		getResources();
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return this.mDrawableIds.length;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.mDrawableIds[position];
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView imageView = new ImageView(this.mContext);
		imageView.setImageResource(this.mDrawableIds[position]);
		imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		imageView.setLayoutParams(new GridView.LayoutParams(90,90));
		return imageView;
	}
	
	public void getResources() {
		TypedArray ar = mContext.getResources().obtainTypedArray(R.array.image_array);
		int length = ar.length();
		mDrawableIds = new Integer[length];
		mDrawables = new String[length];
		for (int i = 0; i < length; i++) {
			mDrawableIds[i] = ar.getResourceId(i, 0);
			mDrawables[i] = ar.getString(i);
		}
		ar.recycle();
	}
}
